<!--20210304    zaq    创建vue文件-->
<!--20210511    zaq    修改界面-->
<!--20210512    zaq    修改校验失败弹窗信息-->
<!--20210522    zaq    照搬新增，实现方法-->


<template>
<!--修改收支记录-->
  <div class="update_list" >
    <el-form rules="rules" ref="form" v-model="form" label-width="80px">
      <el-form-item label="金额" prop="Money">
        <el-input v-model="form.count"
                  style="width: 230px"
                  oninput="value=value.replace(/[^\d.]/g,'')"
                  placeholder="请输入"
        ></el-input>*
      </el-form-item>

      <el-form-item label="时间">
        <!--        <el-col :span="11">-->
        <!--          <el-date-picker type="date" placeholder="选择日期" v-model="form.outputDate" style="width: 230px;"></el-date-picker>-->
        <!--        </el-col>-->
        <!--        <el-col class="line" :span="2">-</el-col>-->
        <!--        <el-col :span="11">-->
        <!--          <el-time-picker placeholder="选择时间" v-model="time" style="width: 100%;"></el-time-picker>-->
        <!--        </el-col>-->

        <el-date-picker
            v-model="form.date"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            align="right"
            :picker-options="pickerOptions">
        </el-date-picker>*
      </el-form-item>


      <el-form-item label="种类">
        <el-select
            v-model="form.kind"
            placeholder="请选择种类"
        >
          <el-option
              v-for="kind in kinds"
              :key="kind.kName"
              :label="kind.kName"
              :value="kind.kName"></el-option>
        </el-select>*
        <el-switch
            v-model="flag"
            active-text="收入"
            active-value="1"
            inactive-text="支出"
            inactive-value="2"
            @change="getFlag($event)"
        ></el-switch>
      </el-form-item>

      <el-form-item label="支付方式">
        <el-select v-model="form.method" placeholder="请选择支付方式" @change="getMethod">
          <el-option
              v-for="pay in pays"
              :key="pay.pName"
              :label="pay.pName"
              :value="pay.pName"></el-option>
        </el-select>*


      </el-form-item>

      <el-form-item label="备注">
        <el-input type="textarea" v-model="form.text" style="width: 230px"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="update">确认修改</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {getKindKind_0000, getKindPay_0000, update_14201} from "../../api";
import moment from "moment";

export default {
  name: "Update_list",
  props:{
    form: {
      sId:-1,
      count:'',
      date:new Date(),
      id:1,
      kind:'',
      method:'',
      text:'',
      flag:2
    },
  },
  data(){
    return {
        rules: {
          Money: [
            {required: true, message: '请填写正确的金额,保留两位小数', pattern: /(^[\d]|^[1-9][\d]*)($|[\.][\d]{0,2}$)/}
          ]
        },
        pays:[{
          pId:-1,
          pName:"",
          uId:-1,
          delete:0
        }],
        kinds:[{
          kId:-1,
          kName:'',
          kFlag:'',
          kBId:-1,
          uId:-1,
          delete:0
        }],
        flag:2,
        pickerOptions: {
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },//时间快捷选项
        time:'',
      }
  },
  methods:{
    //获得数据
    setForm(body){
      this.form=body
      this.flag=body.flag

      console.log('setForm:'+body)
    },
    //获得支付方式
    getKindPay(){
      const _this=this
      var body={
        kId:-1,
        kName:'',
        kFlag:'',
        kBId:-1,
        uId:sessionStorage.getItem("uId"),
        delete:0
      }
      getKindPay_0000(body).then(resp=>{
        _this.pays=resp
        console.log(resp)
      }).catch(error=>{
        this.$message({
          type: 'error',
          message: '获取支付方式失败：'+error
        });
      })
    },
    //获得支付类型
    getKindKind(flag){
      const _this=this
      var uId=sessionStorage.getItem("uId")
      var kBId=sessionStorage.getItem("kBId")
      var kind={
        kId:-1,
        kName:'',
        kFlag:flag,
        kBId:kBId,
        uId:uId,
        delete:0
      }
      getKindKind_0000(kind).then(resp=>{
        _this.kinds=resp
      }).catch(error=>{
        this.$message({
          type: 'error',
          message: '获取支付类型失败：'+error
        });
      })
    },
    getFlag(){
      let flag=this.flag.valueOf()
      this.form.kind=null
      if(flag==1){
        console.log('收入')
        this.form.flag=1

        this.getKindKind(1)
      }else {
        console.log('支出')
        this.form.flag=2
        this.getKindKind(2)
      }
      const _this=this;
    },
    update() {
      //修改日期格式
      const _this=this;
      var uDate=moment(this.form.date).format('yyyyMMDD');
      var uTime=moment(this.form.date).format('HH:mm:ss');

      let input = {
        sId:_this.form.sId,
        sNumber:this.form.count,
        sDate:parseInt(uDate),
        sTime:uTime.toString(),
        sKind:this.form.kind,
        sPay:this.form.method,
        sText:this.form.text,
        sFlag:this.form.flag,
        uId:sessionStorage.getItem("uId"),
        bId:sessionStorage.getItem("bId"),
        gId:sessionStorage.getItem("gId")
      }


      let commit = 0//判断是否能提交
      let msg = ''
      if (input.sKind == null || input.sKind === "") {
        commit = 1
        msg = msg + "种类  "
      }
      if (input.sNumber == null || input.sNumber === "") {
        commit = 1
        msg = msg + "金额  "
      }
      if (input.sTime == null || input.sTime === "") {
        commit = 1
        msg = msg + "时间  "
      }
      if (input.sPay == null || input.sPay === "") {
        commit = 1
        msg = msg + "支付方式  "
      }


      if (commit === 1) {
        this.$alert( msg+'不能为空', '错误!!!', {
          confirmButtonText: '知道了',
          callback: action => {
            this.$message({
              type: 'info',
              message: `请完善收支信息`
            });
          }
        });
      }else {
        console.log(input)
        update_14201(input).then(resp=>{
          if(resp.code>0){
            this.$message({
              type: 'success',
              message: `修改成功`
            });
            _this.$parent.$parent.$parent.$parent.$parent.$parent.$parent.$parent.updateVisible=false
            _this.$parent.$parent.$parent.$parent.$parent.$parent.$parent.$parent.select()
          }else {
            this.$message({
              type: 'error',
              message: `失败：`+resp.note
            });
          }
        }).catch(error=>{
          this.$message({
            type: 'error',
            message: `失败：`+error
          });
        })
      }
    }
  },
  mounted() {

  },
  created() {
    this.getKindKind(this.form.flag)
    this.getKindPay()
  }
}
</script>

<style scoped>
.update_list{
  text-align: left;
}
</style>